Repository navigation
feat: add prefer-includes-over-regex-test rule - #111
Merged
Merged
Conversation
…string methods
`/foo/.test(s)` is the regex engine compiling, executing, and tearing
down a state machine just to ask "does s contain 'foo'?". When the
pattern has no metacharacters or flags, `s.includes('foo')` does the
same thing without the regex machinery.
Autofixes `<RegExpLiteral>.test(s)` whose pattern has no flags and no
metacharacters into the equivalent string method:
- `/foo/.test(s)` → `s.includes("foo")`
- `/^foo/.test(s)` → `s.startsWith("foo")`
- `/foo$/.test(s)` → `s.endsWith("foo")`
- `/^foo$/.test(s)` → `s === "foo"`
Skips patterns containing any of `\\.*+?()[]{}|^$` (after stripping
anchors), regex flags (`/i`, `/g`, …), empty bodies, dynamic
`RegExp(...)` constructors, and non-string receivers. Wraps the
test argument in parens when it's a logical/conditional/await/etc.
expression to preserve precedence.
Caveat: `regex.test(x)` coerces `x` to string while `x.includes(...)`
throws on non-strings. The rule cannot detect that without types;
shipping it as opt-in.
43081j
reviewed
May 2, 2026
43081j
approved these changes
May 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
New
prefer-includes-over-regex-testrule: when a<regex>.test(s)has no metacharacters and no flags, the regex engine work is pure overhead —s.includes('foo')(orstartsWith/endsWith/===) does the same thing without compiling and tearing down a state machine.A real fix this catches (knip
src/plugins/gatsby/index.ts):Behaviour
Autofixes the four shapes:
/foo/.test(s)s.includes("foo")preferIncludes/^foo/.test(s)s.startsWith("foo")preferStartsWith/foo$/.test(s)s.endsWith("foo")preferEndsWith/^foo$/.test(s)s === "foo"preferEqualsSkips:
\.*+?()[]{}|^$after stripping anchors/i,/g, …) since they change semantics//,/^$/,/^/,/$/)new RegExp(...).test(...)(could be anything at runtime).test()with extra argumentsWraps the test argument in parens when needed to preserve precedence (logical, conditional, await, TS cast, etc.).
Caveat
regex.test(x)coercesxto string;x.includes(…)throws on non-strings. The rule has no way to detect non-string receivers without types, so this can produce broken code forregex.test(maybeNumber). In practice almost all real.test()callers pass strings, but the risk is the reason this lands as opt-in (not inrecommended).Test plan
npm test— 706 tests pass (16 in this rule)npm run buildnpm run lint